package com.quicksignin.controller;

import com.quicksignin.common.Const;
import com.quicksignin.common.ServerResponse;
import com.quicksignin.pojo.User;
import com.quicksignin.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by GHD on 2018/10/10.
 */
@Controller
@RequestMapping("")
public class LoginController {

    @Autowired
    IUserService iUserService;


    //跳转到登录页面
    @GetMapping(value = "/login")
    public String showLogin() {
        System.out.println("展示登录页面");
        return "login";
    }

    //跳转到注册页面
    @GetMapping("/register")
    public String showRegister() {
        return "register";
    }

    //用户登录
    @RequestMapping(value = "/loginsub", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> login(String teacherAccount, String teacherPassword, HttpSession session) {
        ServerResponse<User> response = iUserService.login(teacherAccount, teacherPassword);
        if (response.isSuccess()) {
            session.setAttribute(Const.CURRENT_USER, response.getData());
        }
        return response;
    }

    //用户注册
    @RequestMapping(value = "/registersub", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> register(String teacherName, String teacherAccount, String teacherPassword) {
//        System.out.println(teacherName+teacherAccount+teacherPassword);
        ServerResponse<User> response = iUserService.register(teacherName,teacherAccount, teacherPassword);
        return response;
    }
}
